define([
	"static/app/tips",
	"UtilDir/util",
	"UtilDir/tool",
    "UtilDir/org/selectOrg",
    "UtilDir/searchBlock",
	"Date","DateCN", "css!DateCss",
	"EasyUI","EasyUI-lang"
    ],function(tips, Util, Tool, SelectOrg, SearchBlock){

    var sysPath =  getServer() + "/static/app/zdyh/yhbusinessAnalysis";

    var yhDisasterFormHtml = sysPath + "/views/yhDisasterForm.html";
	var getYhDisasterController = function(){
		return getServer() + "/yh/yhAnalysisQuery";
	};

	/**
	 * 页面初始化
	 */
	var init = function(){
        initSearchBlock();
        queryBtnBind();
		initcreateProvince();
		//initEcharts();
	};
    var initcreateProvince = function () {
        var html = "";
        $("#citySelect").append(html);
        $("#areaSelect").append(html);
        $.ajax({
            url: getServer()+"/divisions/root/subdivisions",
            contentType: "application/json",
            type: "get",
            success: function (data) {
                $.each(data, function (idx, item) {
                    html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                });
                $("#provinceSelect").append(html);
            }
        });
        $("#provinceSelect").change(function () {
            var html = '<option value="" disabled selected style="display:none;">请选择</option>';
            $("#citySelect option").remove();
            $("#citySelect").append(html);
            $("#areaSelect option").remove();
            $("#areaSelect").append(html);
            var divisionId = $(this).find("option:selected").attr("exid");
            if ($(this).val() == "北京市" || $(this).val() == "天津市" || $(this).val() == "上海市" || $(this).val() == "重庆市") {
                $("#citySelect").append("<option value='" + $(this).val() + "' exid='" + divisionId + "'>" + $(this).val() + "</option>");
                return;
            }
            $.ajax({
                url: getServer()+"/divisions/" + divisionId + "/subdivisions",
                contentType: "application/json",
                type: "get",
                success: function (data) {
                    $.each(data, function (idx, item) {
                        html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                    });
                    $("#citySelect").append(html);
                }
            });
        });
        $("#citySelect").change(function () {
            if ($(this).val() == "") return;
            var html = '<option value="" disabled selected style="display:none;">请选择</option>';
            $("#areaSelect option").remove();
            $("#areaSelect").append(html);
            var divisionId = $(this).find("option:selected").attr("exid");
            $.ajax({
                url: getServer()+"/divisions/" + divisionId + "/subdivisions",
                contentType: "application/json",
                type: "get",
                success: function (data) {
                    $.each(data, function (idx, item) {
                        html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                    });
                    $("#areaSelect").append(html);
                }
            });
        });
    }
	var initSearchBlock = function(){
        SearchBlock.init("searchBlock");
	};
	var queryBtnBind = function(){
        $("#btnSearch").click(function () {
            initEcharts();
        });
        $("#btnReset").click(function () {
			$("#yhCodeCondition").val("");
			$("#yhUnitCondition").val("");
			$("#yhProjectNameCondition").val("");
			$("#yhProjectTaskCondition").val("");
            $("#provinceSelect").val("");
            $("#citySelect").val("");
            $("#areaSelect").val("");
			$("#yhConstructionScaleCondition").val("");
            $("#yhEarthFortificationSelect").val("");
            $("#yhFieldTypeSelect").val("");
            $("#yhLotTypeSelect").val("");
            $("#evaluationResultSelect").val("");
		});
	};
	var sign = "1";
	$("#citylab").on('shown.bs.tab', function (e) {
        sign = "1";
        initEcharts();
    });
    $("#rurallab").on('shown.bs.tab', function (e) {
        sign = "2";
        initEcharts();
    });
    $("#alllab").on('shown.bs.tab', function (e) {
        sign = "";
        initEcharts();
    });
	var initEcharts = function () {
		require(["Echarts"],function(echarts){
			// 基于准备好的dom，初始化echarts实例
			var myChartFieldType = echarts.init(document.getElementById('countYhFieldType'));
			var myChartLotType = echarts.init(document.getElementById('countYhLotType'));
			var myChartNowEarthQh = echarts.init(document.getElementById('countYhNowEarthQh'));
			var myChartFortification = echarts.init(document.getElementById('yhEarthFortification'));
			var myChartIntensity = echarts.init(document.getElementById('yhEarthIntensity'));
			var myChartExistingVirus = echarts.init(document.getElementById('yhExistingVirus'));
			var myChartCountYhEarth = echarts.init(document.getElementById('queryCountYhEarth'));
			var myChartEvaluationResult = echarts.init(document.getElementById('evaluationResult'));
			var params = {
				type:'商业中心',
                province: $("#provinceSelect").val(),
                city: $("#citySelect").val(),
                area: $("#areaSelect").val(),
                sign: sign,
			}
			var getLegendData = function(data) {
				let res = []
				data.map(item => {
					res.push(item.name)
				})
				return res
			}
			if ($("#provinceSelect").val()=='' || $("#provinceSelect").val()==null){
				Util.alert('请选择省！');
				return;
			}
 			$.ajax({
				url: getYhDisasterController() + "/analysisYhEarthFortification",
				contentType: "application/json",
				type: "get",
				data: params,
				success: function (data) {
					// console.log(data)
					var countYhFieldTypeData = data.countYhFieldType || [] //场地类型
					var countYhLotTypeData = data.countYhLotType || [] //地段类型
					var countYhNowEarthQhData = data.countYhNowEarthQh || [] // 现抗震设防烈度
					var yhEarthFortificationData = data.yhEarthFortification || [] //抗震设防类别
					var yhEarthIntensityData = data.yhEarthIntensity || [] // 原抗震设防烈度
					var yhExistingVirusData = data.yhExistingVirus || [] //现存病害
					var queryCountYhEarth = data.queryCountYhEarth || [] //原抗震设防烈度比较
					var evaluationResult = data.evaluationResult || [] // 评估结果
					var otherSettings = {
						tooltip: {
							formatter: '({d}%)',
							textStyle: {
								fontSize: 18
							}
						},	
						toolbox: {
							feature: {
								saveAsImage: {},
								restore: {},
							}
						},
					}
					var titleSettings = {
						// text: '',
						left: 'center'
					}
					var seriesSettings = {
						type: 'pie',
						// data: countYhFieldTypeData,
						label: {
							normal: {
								postion: 'outer',
								formatter: function(seriesIndex) {
								if (seriesIndex.name.length > 10) {
										
										let name1 = seriesIndex.name.slice(0,6)
										let name2 = seriesIndex.name.slice(6,seriesIndex.name.length-1)
										return `${name1} \n ${name2} \n  ${seriesIndex.value} : (${seriesIndex.percent}%)`
									} else {
										return `${seriesIndex.name} \n ${seriesIndex.value} : (${seriesIndex.percent}%)`
									}
								} 
							},
							emphasis: {
								label: {
									fontSize: 20
								}
							},
						},
						radius: [0,"50%"]
					}
					var legendSettings = {
						// data: getLegendData(countYhFieldTypeData),
						type: 'scroll',
						orient: 'vertical',
						right: '0',
						top: '50',
						// bottom: 0,
						textStyle: {
							fontSize: 10,
						}
					}
					var options = {
						myChartFieldType: {
							...otherSettings,
							title: { text:'场地类型', ...titleSettings },
							legend: { data: getLegendData(countYhFieldTypeData), ...legendSettings, },
							series: [{ data: countYhFieldTypeData, ...seriesSettings}]
						},
						myChartLotType: {
							...otherSettings,
							title: { text:'地段类型', ...titleSettings },
							legend: { data: getLegendData(countYhLotTypeData), ...legendSettings, },
							series: [{ data: countYhLotTypeData, ...seriesSettings}]
						},
						myChartNowEarthQh: {
							...otherSettings,
							title: { text:'现抗震设防烈度', ...titleSettings },
							legend: { data: getLegendData(countYhNowEarthQhData), ...legendSettings, },
							series: [{ data: countYhNowEarthQhData, ...seriesSettings}]
						},
						myChartFortification: {
							...otherSettings,
							title: { text:'抗震设防类别', ...titleSettings },
							legend: { data: getLegendData(yhEarthFortificationData), ...legendSettings, },
							series: [{ data: yhEarthFortificationData, ...seriesSettings}]
						},
						myChartIntensity: {
							...otherSettings,
							title: { text:'原抗震设防烈度', ...titleSettings },
							legend: { data: getLegendData(yhEarthIntensityData), ...legendSettings, },
							series: [{ data: yhEarthIntensityData, ...seriesSettings}]
						},
						myChartExistingVirus: {
							...otherSettings,
							title: { text:'现存病害', ...titleSettings },
							legend: { data: getLegendData(yhExistingVirusData), ...legendSettings, },
							series: [{ data: yhExistingVirusData, ...seriesSettings}]
						},
						myChartCountYhEarth: {
							...otherSettings,
							title: { text:'原抗震设防烈度与现抗震设防原抗震设防烈度比较', ...titleSettings },
							legend: { data: getLegendData(queryCountYhEarth), ...legendSettings, },
							series: [{ data: queryCountYhEarth, ...seriesSettings}]
						},
						myChartEvaluationResult: {
							...otherSettings,
							title: {text:'评估结果', ...titleSettings},
							legend: { data: getLegendData(evaluationResult), ...legendSettings, },
							series: [{ data: evaluationResult, ...seriesSettings}]
						}
					}
					myChartFieldType.setOption(options.myChartFieldType);
					// myChartLotType.setOption(options.myChartLotType);
					myChartNowEarthQh.setOption(options.myChartNowEarthQh);
					myChartFortification.setOption(options.myChartFortification);
					myChartIntensity.setOption(options.myChartIntensity);
					myChartExistingVirus.setOption(options.myChartExistingVirus);
					myChartCountYhEarth.setOption(options.myChartCountYhEarth);
					myChartEvaluationResult.setOption(options.myChartEvaluationResult)
				}
			});
		})
	}
	return {
		init:init
	};
});
